﻿$.SaveForm = function (options) {
    var defaults = {
        url: "",
        param: [],
        type: "post",
        dataType: "json",
        loading: "正在处理数据...",
        success: null,
        close: true
    };
    options = $.extend(defaults, options);

    Loading(true, options.loading);

    if ($('[name=__RequestVerificationToken]').length > 0) {
        options.param["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();
    }

    Vue.http.post(options.url, options.param,
        {
            emulateJSON: true,
            before: function () {
                Loading(true, options.loading);
            }
        }).then(function (response) {
            dialogMsg('操作成功。', 1);
            options.success(response.data);
            if (options.close === true) {
                dialogClose();
            }
        });
};

$.SetForm = function (options) {
    var defaults = {
        url: "",
        param: [],
        type: "get",
        dataType: "json",
        success: null,
        async: false
    };
    var options = $.extend(defaults, options);
    $.ajax({
        url: options.url,
        data: options.param,
        type: options.type,
        dataType: options.dataType,
        async: options.async,
        success: function (data) {
            if (data != null && data.type == "3") {
                dialogAlert(data.message, -1);
            } else {
                options.success(data);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            dialogMsg(errorThrown, -1);
        },
        beforeSend: function () {
            Loading(true);
        },
        complete: function () {
            Loading(false);
        }
    });
};

$.RemoveForm = function (options) {
    var defaults = {
        msg: "注：您确定要删除吗？该操作将无法恢复!",
        loading: "正在删除数据...",
        url: "",
        param: [],
        type: "post",
        dataType: "json",
        success: null
    };

    options = $.extend(defaults, options);

    dialogConfirm(options.msg,
        function (r) {
            if (r) {
                Loading(true, options.loading);

                if ($('[name=__RequestVerificationToken]').length > 0) {
                    options.param["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();
                }

                Vue.http.post(options.url, options.param,
                    {
                        emulateJSON: true,
                        before: function () {
                            Loading(true, options.loading);
                        }
                    }).then(function (response) {
                        dialogMsg('删除成功。', 1);
                        options.success(response.data);

                        //chenjd 2017-8-10 15:10:04
                        //此处不需要关闭confirm对话框，dialog多个时layer会自动关闭之前dialog。
                    });
            }
        });
};

$.ConfirmAjax = function (options) {
    var defaults = {
        msg: "提示信息",
        loading: "正在处理数据...",
        url: "",
        param: [],
        type: "post",
        dataType: "json",
        success: null
    };
    options = $.extend(defaults, options);

    dialogConfirm(options.msg,
        function (r) {
            if (r) {
                Loading(true, options.loading);

                if ($('[name=__RequestVerificationToken]').length > 0) {
                    options.param["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();
                }

                Vue.http.post(options.url, options.param,
                    {
                        emulateJSON: true,
                        before: function () {
                            Loading(true, options.loading);
                        }
                    }).then(function (response) {
                    dialogMsg('操作成功。', 1);
                    options.success(response.data);
                });
            }
        });
};

$.ExistField = function (controlId, url, param) {
    var $control = $("#" + controlId);
    if (!$control.val()) {
        return false;
    }
    var data = {
        keyValue: request('keyValue')
    };
    data[controlId] = $control.val();
    var options = $.extend(data, param);
    $.ajax({
        url: url,
        data: options,
        type: "get",
        dataType: "text",
        async: false,
        success: function (data) {
            if (data.toLocaleLowerCase() == 'false') {
                ValidationMessage($control, '已存在,请重新输入');
                $control.attr('fieldexist', 'yes');
            } else {
                $control.attr('fieldexist', 'no');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            dialogMsg(errorThrown, -1);
        }
    });
};





